package edu.tms.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;

import edu.tms.bean.*;
import edu.tms.dao.*;
import net.easymvc.annotation.Encoding;
import net.easymvc.annotation.Param;
import net.easymvc.core.Action;


public class CounselorAction extends Action{
	/**
	 * 教师所教对应班级信息
	 * @param tchNo
	 * @return
	 */
	@Encoding("utf-8")
	public String queryAssignmentInfo(
			@Param("pageNoTop")Integer pageNoTop,
			@Param("pageNo") Integer pageNo) {
		//TODO学生选择和查询对应班级班干部，提交班干部信息
		User user = (User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		int tchNo = user.getUserId();
		if(pageNo == null || pageNo < 1) pageNo = 1;
		if(pageNoTop == null || pageNoTop < 1) pageNoTop = 1;
		//实例化班级类
		ClassInfoDaoImp ci = new ClassInfoDaoImp();
		//实例化学生信息类
		StudentDaoImp sdi = new StudentDaoImp();
		//实例化职务名称类
		RoleDaoImp rdi = new RoleDaoImp();
		//获取班级名称
		String className = (String) getRequest("className");
		//获取查找的学生姓名
		String findStuName = (String)getRequest("findStuName");
		try {
			//根据教师编号获取班级信息
			List<ClassInfo> cInfo = ci.findCounselor(tchNo);
			//获取所有的职位 用于页面展示
			List<Role> role = rdi.findAllNotPage();
			//后续需要在刚进入网页时有参数，所以需要一个默认班级，就获取第一个班级名称
			String firstClassName = cInfo.get(0).getClassName();
			//创建数组(获取班级上的班委信息)
			List<Student> stu = null;
			//创建数组(获取该班级所有的学生信息或者某个学生的信息)
			List<Student> stuAll = null;
			//创建数组(用于获取下面板块总页数)
			List<Student> stuPage = null;
			//创建数组(用于获取上面板块总页数)
			List<Student> stuTopPage = null;
			if(className==null || className==""){
				//由于获取到的班级有可能没有数据所以直接设置成软件一班，后面删除
				//获取拥有职位并且属于该班级的学生
				stu = sdi.findAllOfClass(firstClassName,pageNoTop,5);
				stuAll = sdi.findAllStuOfClass(firstClassName,pageNo,5);
				stuPage = sdi.findAllStuOfClassInPage(firstClassName);
				stuTopPage = sdi.findAllOfClassInPage(firstClassName);
			}else{
				//获取拥有职位并且属于该班级的学生
				stu = sdi.findAllOfClass(className,pageNoTop,5);
				stuPage =  sdi.findAllStuOfClassInPage(className);
				stuTopPage = sdi.findAllOfClassInPage(className);
				//获取所有学生
				if(findStuName==null || findStuName==""){
					//只根据班级名称查询
					stuAll = sdi.findAllStuOfClass(className,pageNo,5);
				}else{
					//根据班级名称和学生名称查询
					stuAll = sdi.findStuOfClass(className,findStuName);
				}
			}
			//获取总页数(上面板块)
			int pageCountTop = stuTopPage.size()/5;
			//当总页数小于1时，设置总页数为1
			if(pageCountTop<1){
				pageCountTop=1;
			}else if(stuTopPage.size()%5!=0){
				pageCountTop++;
			}
			//获取总页数(下面板块)
			int pageCount = stuPage.size()/5;
			//当总页数小于1时，设置总页数为1
			if(pageCount<1){
				pageCount=1;
			}else if(stuPage.size()%5!=0){
				pageCount++;
			}
			//设置网页属性
			setRequest("cInfo", cInfo);
			setRequest("stu",stu);
			setRequest("role",role);
			setRequest("className",className);
			setRequest("stuAll",stuAll);
			setRequest("pages",pageCount);
			setRequest("pageNo", pageNo);
			setRequest("pageNoTop", pageNoTop);
			setRequest("pageCountTop",pageCountTop);
			return "/c/assignment.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 撤销学生职务
	 * @return
	 */
	@Encoding("utf-8")
	public String delStuZ(Student stu,@Param("stuId")int stuId) {
		StudentDaoImp sdi = new StudentDaoImp();
		try {
			stu.setStuId(stuId);
			stu.setRoleName("学生");
			sdi.updateRoleInStu(stu);
			return "/counselor/queryAssignmentInfo.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 授予学生职务
	 * @param stu_id
	 * @return
	 */
	@Encoding("utf-8")
	public String giveStuZ(Student stu,
			@Param("stuId")Integer stuId,
			@Param("roleId")Integer roleId) {
		StudentDaoImp sdi = new StudentDaoImp();
		try {
			stu.setStuId(stuId);
			stu.setRoleName(sdi.findRoleName(roleId));
			sdi.updateRoleInStu(stu);
			return "/counselor/queryAssignmentInfo.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 辅导员显示所有活动信息
	 * @param classNo
	 * @param pageNo
	 * @return
	 */
	@Encoding("utf-8")
	public String classactive(
			@Param("cName")String className,
			@Param("pageNo") Integer No) {
		ClassActivityDaoImp cActDao=new ClassActivityDaoImp();//活动dao
		ClassInfoDaoImp classDao=new ClassInfoDaoImp();//班级dao
		List<ClassActivity> activityList=null;
		User user=(User)getSession("curUser");
		int pages=0;//总页数
		Long count=null;//总条目
		int pageNo=1;//当前页数
		if(No!=null)pageNo=No;
		System.out.println("cname:"+className);
		try {
			if( className==null || className.equals("--请选择--") || className.equals("")){
				//查询所有活动和班级信息
				activityList=cActDao.findAllInfo(user.getUserId(),(pageNo-1)*5,5);
				count=cActDao.findAllCount(user.getUserId());
			}else{
				//根据班级名称查询活动
				activityList=cActDao.findByName(user.getUserId(),className,(pageNo-1)*5,5);
				count=cActDao.findByNameCount(user.getUserId(),className);
			}
			
			if(count!=null && count%5!=0){
				pages=(int) (count/5)+1;
			}else{
				pages=(int) (count/5);
			}
			if(className!=null && className.equals("")){
				setRequest("className",className);
			}
			List<ClassInfo> classList=classDao.findAllInfo(user.getUserId());
			setRequest("pages",pages);
			setRequest("pageNo",pageNo);
			setRequest("activityList",activityList);
			setRequest("classList",classList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/c/activity.jsp";
	}
	
	/**
	 * 辅导员显示所有学生历程信息
	 * @param classNo
	 * @param pageNo
	 * @return
	 */
	@Encoding("utf-8")
	public String schoolLife(
			@Param("condition")String Condition,
			@Param("pageNo") Integer No){
		User user=(User)getSession("curUser");//获取当前登录的辅导员
		SchoolLifeDaoImp  lifeDao=new SchoolLifeDaoImp();//创建SchoolLifeDaoImp
		//首先查询所有历程转到网页
		List<SchoolLife> schoolList=null;
		int pages=0;//总页数
		Long count=null;//总条目
		int pageNo=1;
		if(No!=null)pageNo=No;
		try {
			if( Condition==null || Condition.equals("--请选择--") || Condition.equals("")){
				schoolList=lifeDao.findAllUid(user.getUserId(),(pageNo-1)*5,5);//首次查询第一个班的学生历程
				count=lifeDao.findAllUidCount(user.getUserId());
			}else{
				schoolList=lifeDao.findByCondition(Condition,user.getUserId(),(pageNo-1)*5,5);
				count=lifeDao.findByConditionCount(Condition,user.getUserId());
			}
			if(count!=null && count%5!=0){
				pages=(int) (count/5)+1;
			}else{
				pages=(int) (count/5);
			}
			if(Condition!=null && Condition.equals("")){
				setRequest("Condition",Condition);
			}
			setRequest("pages",pages);
			setRequest("pageNo",pageNo);
			setRequest("schoolList",schoolList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/c/course.jsp";
	}
	/**
	 * 查看一条活动内容
	 * @param classActivityId
	 * @return
	 */
	@Encoding("utf-8")
	public String showClassActive(@Param("aid")Integer classActivityId){
		ClassActivityDaoImp cActDao=new ClassActivityDaoImp();//活动dao
		PictureDaoImp pDao=new PictureDaoImp();//照片dao
		try {
			ClassActivity ca=cActDao.findByAid(classActivityId);
			List<Picture> pList=pDao.findUrl(ca.getAlbumId());
			String listJson = JSON.toJSONString(pList);
			listJson = "{\"pList\":["+listJson+"],"+"\"at\":\""+ca.getActivityTitle()+"\","+"\"slc\":\""+ca.getActivityContent()+"\"}";
			return "ajax:"+listJson;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	@Encoding("utf-8")
	public String showSchoolLife(@Param("sid")Integer schoolLifeId){
		SchoolLifeDaoImp  lifeDao=new SchoolLifeDaoImp();
		PictureDaoImp pDao=new PictureDaoImp();//照片dao
		try {
			SchoolLife slife=lifeDao.findBySid(schoolLifeId);
			List<Picture> pList=pDao.findUrl(slife.getAlbumId());
			String listJson = JSON.toJSONString(pList);
			listJson = "{\"pList\":["+listJson+"],"+"\"at\":\""+slife.getActivityTitle()+"\","+"\"slc\":\""+slife.getSchoolLifeContent()+"\"}";
			return "ajax:"+listJson;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	/**
	 * 添加评测
	 * @throws ParseException 
	 */
	@Encoding(value ="utf-8")
	public String courseAssessment(
			@Param("classname")String classnames,
			@Param("startime")String stime,
			@Param("endtime")String etime,
			@Param("tchname")String teacher,
			@Param("coursename")String coursen){

		SimpleDateFormat sfd =new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date st=sfd.parse(stime);
			Date et=sfd.parse(etime);
			AssessmentInfo as= new AssessmentInfo();
			as.setTchName(teacher);
			as.setClassName(classnames);
			as.setCourseName(coursen);
			as.setAssessmentStartTime(st);
			as.setAssessmentEndTime(et);
			as.setCreateTime(new Date());
			AssessmentInfoDaoImp adao =new AssessmentInfoDaoImp();
			adao.save(as);
			System.out.println("ok");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/counselor/huoqu.do";
	}
	
	/**
	 * 加载可添加信息
	 * @return
	 * @throws Exception
	 */
	@Encoding(value ="utf-8")
	public String huoqu() throws Exception{
		//班级名字获取
		List<ClassInfo> c=new ArrayList<ClassInfo>();
		ClassInfoDaoImp cdao=new ClassInfoDaoImp();
		c=cdao.findClass();
		setRequest("classname", c);
		EvaluatingDaoImp eldao =new EvaluatingDaoImp();
		for(int j = 1; j < 4; j++){
			List<Long> num = new ArrayList<>();
			num.add(eldao.conDiTionalQuery("master_level",j));
			num.add(eldao.conDiTionalQuery("feel_level",j));
			num.add(eldao.conDiTionalQuery("attraction_level",j));
			num.add(eldao.conDiTionalQuery("duty_level",j));
			num.add(eldao.conDiTionalQuery("accident_level",j));
			num.add(eldao.conDiTionalQuery("discipline_level",j));
			num.add(eldao.conDiTionalQuery("aggign_level",j));
			num.add(eldao.conDiTionalQuery("answer_level",j));
			setRequest("num"+j, num);
		}
		List<Evaluating> es= new ArrayList<Evaluating>();
		es=eldao.findAll(1,5);
		setRequest("assessment", es);
		return "/c/evaluating.jsp"; 
	}
	/**
	 * 根据班级进行查询老师
	 * @throws Exception 
	 */
	@Encoding(value ="utf-8")
	public String findTeacher(
			@Param("chuanzhi")String czhi,
			@Param("chuandier")String zhier){
		System.out.println("接收的课程"+czhi);
		System.out.println("接收的班级"+zhier);
		Teacher cr = null;
		TeacherDaoImp cdao=new TeacherDaoImp();
		try {
			cr=cdao.findtch(zhier,czhi);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("获取的老师"+cr);
		String clist=JSON.toJSONString(cr);
		return "ajax:"+clist;
	}
	/**
	 * 根据班级查询课程
	 * @throws Exception 
	 */
	@Encoding(value ="utf-8")
	public String findCourseer(@Param("chuanzhi")String czhi){
		List<Course> cr =new ArrayList<Course>();
		CourseDaoImp cdao=new CourseDaoImp();
		try {
			cr=cdao.findCourse(czhi);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String clist=JSON.toJSONString(cr);
		return "ajax:"+clist;
	}

	/**
	 * 查看当前的最新评测信息
	 * @throws Exception 
	 */
	@Encoding(value ="utf-8")
	public String classevaluatinginfo(@Param("ctions")String tiaojian) throws Exception{
		System.out.println("获取的"+tiaojian);
		List<AssessmentInfo> ass=new ArrayList<AssessmentInfo>();
		AssessmentInfoDaoImp asdao=new AssessmentInfoDaoImp();
		ass=asdao.findassement(tiaojian);
		System.out.println("ass"+ass);
		String list=JSON.toJSONString(ass);
		System.out.println("list:"+list);
		return "ajax:"+list;
	}
	
	/**
	 * 显示学生信息
	 * @param className
	 * @param student
	 * @param pageNo
	 * @return
	 */
	@Encoding("UTF-8")
	public String stuInfo(@Param(value="className")String className,
			@Param(value="student")String student,
			@Param(value="pageNo")Integer pageNo){
		User user=(User) getSession("curUser");
		if(user==null)return "redirect:/login.html";
		if(pageNo == null || pageNo < 1)pageNo = 1;
		StudentDaoImp stu= new StudentDaoImp();
		MajorDaoImp maj=new MajorDaoImp();
		ClassInfoDaoImp cla= new ClassInfoDaoImp();
		try {
			//接受dao传送过来的数组
			List<Major> listMajor=maj.findAll(user.getUserId());
			List<ClassInfo> listClassInfo=cla.findAll(user.getUserId());
			setRequest("cla", listClassInfo);
			if(student==null || student.length()==0){
				if(className==null || className.length()==0){
					List<Student> listStudent=stu.findByTchId(user.getUserId(), pageNo, PAGE_SIZE);
					long pages=stu.findByTchId(user.getUserId());
					if(listStudent==null)setRequest("error_msg","信息出错请重试");
					setRequest("stu", listStudent);
					setRequest("a","a");
					setRequest("pages", pages);
				}
				else if(className!=null){
					System.out.println(className);
					List<Student> listStudent=stu.findByCls(className, pageNo, PAGE_SIZE);
					long pages=stu.findClaCount(className);
					if(listStudent==null)setRequest("error_msg","信息出错请重试");
					setRequest("stu", listStudent);
					setRequest("pages", pages);
				}
			}
			else{
				List<Student> listStudent=stu.findAll(student, pageNo, PAGE_SIZE);
				if(listStudent==null)setRequest("error_msg","信息出错请重试");
				setRequest("stu", listStudent);
			}
			//转发到网页中
			setRequest("maj", listMajor);
			setRequest("pageNo", pageNo);
			return "/c/stuInfo.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/c/stuInfo.jsp";
	}
	
	//修改学生信息
	@Encoding("utf-8")
	public String updateStuInfo(@Param(value="studentId")String studentId,
			@Param(value="updateStu")Integer id,
			@Param(value="updateValue")String value){
		StudentDaoImp stuDao=new StudentDaoImp();
		Student stu;
		try {
			stu = stuDao.findById(studentId);
			stu.setStuNo(studentId);
			if(id==1)stu.setStuClassName(value);
			else if(id==2)stu.setStuSquadId(Integer.parseInt(value));
			else if(id==3)stu.setStuDormName(value);
			else if(id==4)stu.setStuPhone(value);
			else if(id==5)stu.setStuAddress(value);
			else if(id==6)stu.setStuQq(value);
			else if(id==7)stu.setStuEmail(value);
			System.out.println(stu);
			int error=stuDao.update(stu);
			if(error==0)setRequest("error_msg","信息出错请重试");
			return stuInfo(null,null,0);
		} catch (Exception e1) {
			e1.printStackTrace();
			return stuInfo(null,null,0);
		}
	}
	
	@Encoding("utf-8")
	public String jion(@Param(value="jionName")String jionName,
			@Param(value="pageNo")Integer pageNo){
		User user=(User) getSession("curUser");
		if(user==null)return "redirect:/login.html";
		if(pageNo == null || pageNo < 1) pageNo = 1;
		//dao
		ApplicationInfoDaoImp app=new ApplicationInfoDaoImp();
		try {
			if(jionName==null || jionName.length()==0){
				List<ApplicationInfo> listapp = app.findAll( pageNo, 10);
				long pages = app.findAllCount();
				if(listapp==null)setRequest("error_msg","信息出错请重试");
				//转发到网页中
				setRequest("app", listapp);
				setRequest("pages", pages);
			}else{
				//接受dao传送过来的数组
				List<ApplicationInfo> listapp = app.findAll(jionName, pageNo, 10);
				long pages=app.findIdCount(jionName);
				System.out.println(listapp);
				if(listapp==null)setRequest("error_msg","信息出错请重试");
				//转发到网页中
				setRequest("app", listapp);
				setRequest("pages", pages);
			}
			setRequest("pageNo",pageNo);
			return "/c/jion.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "/c/jion.jsp";
		}
	}

	@Encoding("utf-8")
	public String updatejion(
			@Param(value="shenqing")Integer id,
			@Param(value="stuName")String stuName){
		ApplicationInfoDaoImp appDao=new ApplicationInfoDaoImp();
		try {
			ApplicationInfo app=appDao.findByName(stuName);
			app.setApplicationState(id);
			int error=appDao.update(app);
			System.out.println(error);
			if(error==0)setRequest("error_msg","信息出错请重试");
			return jion(null,0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jion(null,0);
	}
	
	
	/**
	 * 查看缴费情况
	 * @param pageNo 页码
	 * @param majorname 专业名
	 * @param className 班级名
	 * @param content 学生名
	 * @return
	 */
	@Encoding("UTF-8")
	public String tuition(@Param(value="pageNo") Integer pageNo,
			@Param(value="majorName")String majorname,
			@Param(value="className")String className,
			@Param(value="content")String content){
		if(pageNo == null||pageNo < 1) pageNo=1;//页码
		User user = (User) getSession("curUser");
		if(user == null)return "redirect:/login.html";
		ClassInfoDaoImp cDao=new ClassInfoDaoImp();
		TuitionDaoImp tDao=new TuitionDaoImp();
		int tchId = user.getUserId();
		try {
			int pages=0;//总页数
			if(className == null || className.length()==0){
				pages=(int) tDao.pageBytchId(tchId, 10);
			}else{
				if(content==null || content.length()==0){
					pages=(int) tDao.pageByMajor(tchId, majorname, className, 10);
				}else{
					pages=(int) tDao.pageByStuName(tchId, majorname, className, content, 10);
				}
			}
			if(pageNo > pages) pageNo = pages;
			List<ClassInfo> cname=cDao.findBytchId(tchId);
			List<Tuition> tuis;
			if(className==null || className.length()==0){
				tuis=tDao.queryBytchId(tchId, pageNo, 10);
			}else{
				if(content==null || content.length()==0){
					tuis=tDao.queryByMajor(tchId, majorname, className, pageNo, 10);
				}else{
					tuis=tDao.queryByStuName(tchId, majorname, className, content, pageNo, 10);
				}
			}
			setRequest("tuis", tuis);
			setRequest("pageNo", pageNo);
			setRequest("pages", pages);
			setRequest("className", cname);
			return "/c/payment.jsp";
		}catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
}
